package org.wheel.Service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wheel.Service.ISzrqExportService;
import org.wheel.dao.SzrqExport;
import org.wheel.dao.SzrqExportDao;
import org.wheel.dao.TUserDao;
import org.wheel.plugins.starter.autoconfigure.DynamicDataSourceProperties;
import org.wheel.plugins.starter.jdbc.pool.service.impl.BaseServiceImpl;
import org.wheel.plugins.toolkit.jdbc.pool.util.LambdaSqlExecutionUtil;
import org.wheel.plugins.toolkit.jdbc.pool.util.SqlExecutionUtil;
import org.wheel.plugins.toolkit.sql.core.LambdaInsertWrapper;
import org.wheel.plugins.toolkit.sql.core.Wrappers;
import org.wheel.plugins.toolkit.sql.enums.Dialect;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lanjf2000
 * @since 2025-10-09
 */
@Service
public class SzrqExportServiceImpl extends BaseServiceImpl<SzrqExportDao, SzrqExport> implements ISzrqExportService {

    @Autowired
    private TUserDao tUserDao;

    @Autowired
    private DynamicDataSourceProperties conn;

//    @PostConstruct
//    public void init(){
//        System.out.println( conn);
//        System.out.println(tUserDao);
//    }

    @Override
    public List<SzrqExport> readData() {
        String filePath = "/home/lanjf2000/Desktop/客户预警信息.xlsx";
        ExcelReader reader = ExcelUtil.getReader(filePath);
        return reader.setSheet(0).readAll(SzrqExport.class);
    }

    @Override
    public void writeData() {
        SqlExecutionUtil.getInstance().executeSqlUpdate(conn.getMultiDatasource().get("local-mysql57"), "truncate table szrq.szrq_export");
        List<SzrqExport> data = readData();
        List<LambdaInsertWrapper<SzrqExport>> list = data.stream().map(item->{
            item.setFbrq(DateUtil.date().toLocalDateTime().toLocalDate());
            return Wrappers.lambdaInsert(SzrqExport.class).insert(item).dialect(Dialect.MYSQL);
        }).toList();
        LambdaSqlExecutionUtil.saveBatch(conn.getMultiDatasource().get("local-mysql57"), list);
    }
}
